package com.example.home.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.home.activity.R;
import com.example.home.viewmodel.home.BrandList;

import java.util.List;

public class HomeBrandAdapter extends BaseAdapter {

    private Context ctx;
    private final List<BrandList> brandLists;

    public HomeBrandAdapter(Context ctx, List<BrandList> brandLists) {
        this.ctx = ctx;
        this.brandLists = brandLists;
    }

    @Override
    public int getCount() {
        return this.brandLists.size();
    }

    @Override
    public Object getItem(int position) {
        return this.brandLists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return this.brandLists.get(position).getId();

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView==null){
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(this.ctx).inflate(R.layout.home_brand_item,null);
            viewHolder.home_brand_img = convertView.findViewById(R.id.home_brand_img);
            viewHolder.home_brand_text = convertView.findViewById(R.id.home_brand_text);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.home_brand_text.setText(this.brandLists.get(position).getName());
        if(this.brandLists.get(position).getPicUrl()!="") {
            Glide.with(convertView.getContext())
                    .load(this.brandLists.get(position).getPicUrl())
                    .into(viewHolder.home_brand_img);
        }else{
            Log.e("TAG", "error" );
        }
        return convertView;
    }

    static class ViewHolder {
        ImageView home_brand_img;
        TextView home_brand_text;
    }

}
